openerp.osbzr_group =function() {
    var instance = openerp;
        openerp.web.data_export = {};

    var QWeb = instance.web.qweb,
        _t = instance.web._t;
     /*instance.web.DataExport.include({
*/

instance.web.search.group = instance.web.search.Input.extend({
    template: 'SearchView.Group',
    _in_drawer: true,

    init: function (parent,custom_filters) {
        this._super.apply(this, arguments);
        this.custom_filters=custom_filters
    },

    start: function () {
        var self = this;
        this.$el
            .on('keypress keydown keyup', function (e) { e.stopPropagation(); })
            .on('click', 'h4', function () {
                self.$el.toggleClass('oe_opened');
            }).on('click', 'button.oe_add_condition', function () {
            }).on('submit', 'form', function (e) {
                e.preventDefault();
                self.commit_search();
            });
        var action_id=instance.client.action_manager.inner_action
        return $.when(
            this._super(),
            new instance.web.Model(this.view.model).call('fields_get', {
                    context: this.view.dataset.context
                }).done(function(data) {
                    self.fields = {
                        id: { string: 'ID', type: 'id', searchable: true }
                    };
                    _.each(data, function(field_def, field_name) {
                        if (field_def.selectable !== false && field_name != 'id') {
                            self.fields[field_name] = field_def;
                        }
                    });
        })).done(function () {
            self.append_proposition();
            self.$el.find('#searchview_extended_prop_op').hidden=true;
        });
    },
    append_proposition: function () {
        var self = this;
        return (new instance.web.search.Group_ExtendedSearchProposition(this, this.fields))
            .appendTo(this.$('ul')).done(function () {
                self.$('button.oe_apply').prop('disabled', false);
            });
    },
    commit_search: function () {
        var children = this.getChildren();
        var group = _.invoke(children, 'get_proposition');
        var self = this;
        var custom_filters=this.drawer.custom_filters.filters;
        var str='按'+group[0].label+'分组 ';
        var filters = new openerp.web.Model('ir.filters');
        var action_id=instance.client.action_manager.inner_action
        this.filters = {};
        var filter = {
            name: str,
            user_id: this.session.uid,
            model_id: this.view.model,
            context: "{'group_by' : '"+group[0].value+"'}",
            domain: '[]',
            is_default: false,
           action_id:action_id.id,
        };
        this.view.query.add({
            category: _t("分组"),
            values: group,
            field: {
                get_context: function () { },
                get_domain: function () { },
                get_groupby: function () {return ["{'group_by' : '"+group[0].value+"'}"]}
            }
        });
        _.invoke(children, 'destroy');
        this.append_proposition();
        this.view.$el.removeClass('oe_searchview_open_drawer');
        this.model = new instance.web.Model('ir.filters');
        return self.model.call('create_or_replace', [filter]).done(function (id) {
            filter.id = id;
            if (self.custom_filters) {
                 self.custom_filters.append_filter(filter);
            };
            self.$el
                .removeClass('oe_opened')
                .find('form')[0].reset();
        });
    },
});

instance.web.SearchViewDrawer.include({
        add_common_inputs: function() {
            var self = this;
            this._super.apply(this, arguments);
            (new instance.web.search.group(this,this.custom_filters));}
});
instance.web.search.Group_ExtendedSearchProposition = instance.web.Widget.extend(/** @lends instance.web.search.ExtendedSearchProposition# */{
    template: 'SearchView.group_extended_search.proposition',
    events: {
        'change .searchview_extended_prop_field': 'changed',
    },
    init: function (parent, fields) {
        this._super(parent);
        this.fields = _(fields).chain()
            .map(function(val, key) {  return _.extend({}, val, {'name': key}); })
            .filter(function (field) {   
                if (field.store&&field.relation_field===undefined && field.type!='many2many'&&field.type!='one2many'){return !field.deprecated && field.searchable; }})
            .sortBy(function(field) {  
                 return field.string;})
            .value();
        this.attrs = {_: _, fields: this.fields, selected: null};
        this.value = null;
    },
    start: function () {
        return this._super().done(this.proxy('changed'));
    },
    changed: function() {
        var nval = this.$(".searchview_extended_prop_field").val();
        if(this.attrs.selected === null || this.attrs.selected === undefined || nval != this.attrs.selected.name) {
            this.select_field(_.detect(this.fields, function(x) {return x.name == nval;}));
        }
    },
    select_field: function(field) {
        var self = this;
        if(this.attrs.selected !== null && this.attrs.selected !== undefined) {
            this.value.destroy();
            this.value = null;
            this.$('.searchview_extended_prop_op').html('');
        }
        this.attrs.selected = field;
        if(field === null || field === undefined) {
            return;
        }
        var type = field.type;
        var Field = instance.web.search.custom_filters.get_object(type);
        if(!Field) {
            Field = instance.web.search.custom_filters.get_object("char");
        }
        this.value = new Field(this, field);
        _.each(this.value.operators, function(operator) {
            $('<option>', {value: ""})
                .text(String(""))
                .appendTo(self.$('.searchview_extended_prop_op'));
        });
        var $value_loc = this.$('.searchview_extended_prop_value').show().empty();
        this.value.appendTo($value_loc);
    },
    get_proposition: function() {
        if (this.attrs.selected === null || this.attrs.selected === undefined)
            return null;
        var field = this.attrs.selected;
        return {
            label: [field.string],
            value: field['name'],
        };

    },
});

}
